#pragma  once 

#include"com.hpp"
class Sock{
  public:
    static int Socket()
    {
      int sock = socket(AF_INET,SOCK_STREAM,0);
      if(sock<0)
      {
        cout<<"socket fail"<<endl;
        exit(2);
      }
      return sock;
    }

    static void Bind(int sock,uint16_t port)
    {
      struct sockaddr_in local;
      memset(&local,0,sizeof(local));
      local.sin_family = AF_INET;
      local.sin_port = htons(port);
      local.sin_addr.s_addr =INADDR_ANY;

      if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
      {

        cout<<"bind fail"<<endl;
        exit(3);
      }
    }
    static void Listen(int sock)
    {
      if(listen(sock,5)<0)
      {
        cout<<"listen fail"<<endl;                            
        exit(4);
      }
    }


    static int Accept(int sock)
    {
      struct sockaddr_in peer;
      socklen_t len = sizeof(peer);
      int fd= accept(sock,(struct sockaddr*)&peer,&len);
      if(fd>=0)
      {
        return fd;
      }

      return -1;
    }

    static void Connect(int sock,string ip,uint16_t port)
    {
      struct sockaddr_in server;
      memset(&server,0,sizeof(server));
      server.sin_family = AF_INET;
      server.sin_addr.s_addr = inet_addr(ip.c_str());
      server.sin_port = htons(port);

     if(connect(sock,(struct sockaddr*)&server,sizeof(server))==0)
     {
        cout<<"connect success!!"<<endl;
     }
     else 
     {
       cout<<"connect fail"<<endl;
       exit(5);
     }
    }

};
